package com.nikkerball.web.converter;

import com.nikkerball.Computation;
import com.nikkerball.web.base.BasePanel;
import com.nikkerball.web.session.ComputationSession;
import com.nikkerball.web.session.StumpsSession;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

/**
 * TODO: Document class
 *
 * @author marius (created on 2010-02-19)
 */
public class ComputationsPanel extends BasePanel {

    // ----- class fields -----

    private static final Logger LOGGER = LoggerFactory.getLogger(ComputationsPanel.class);

    // ----- instance fields -----

    private ComputationSession computationSession;

    // ----- constructors -----

    public ComputationsPanel(String id) {
        super(id);

        computationSession = ((StumpsSession) getSession()).getComputationSession();

        final Properties labels = new Properties();
        loadLabels(labels);

        final Form computationForm = new Form("computationForm");
        computationForm.setOutputMarkupId(true);

        ListView<Computation> computationView = new ListView<Computation>("computationView", getComputations()) {
            protected void populateItem(ListItem<Computation> listItem) {

                final Computation computation = listItem.getModelObject();

                String description = computation.getDescription(labels);

                Label computationLabel = new Label("computationLabel", description);
                AjaxButton removeButton = new AjaxButton("removeButton") {
                    @Override
                    protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                        getComputations().remove(computation);
                        target.addComponent(computationForm);
                    }
                };
                listItem.add(computationLabel);
                listItem.add(removeButton);
            }
        };

        computationForm.add(computationView);

        add(computationForm);

    }

    // ----- public methods -----

    public List<Computation> getComputations() {
        return computationSession.getComputations();
    }

    // ----------

    private void loadLabels(Properties labels) {
        // TODO: I guess this can be done cleaner
        try {
            InputStream stream = this.getClass().getResourceAsStream("ComputationPage.properties");
            labels.load(stream);
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }

}
